import Vue from 'vue'
import Router from 'vue-router'
import { getUserId } from '@/utils/auth'
import Cookies from 'js-cookie'
Vue.use(Router)

/* Layout */
import Layout from '@/layout'

import routing from '@/api/routing'
/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */


var constantRoutes = [{
        path: '/login',
        component: () =>
            import ('@/views/login/index'),
        hidden: true
    },

    {
        path: '/404',
        component: () =>
            import ('@/views/404'),
        hidden: true
    },



    /* {
        path: '/test',
         component: Layout, 
         redirect: '/test/test',
         children: [{
          path: 'test',
          name: '测试',
          component: () => import('@/views/test'),
          meta: { title: '测试', icon: 'home',   }
        }]
      },  */

    /*   {
        path: '/',
        component: Layout,
        redirect: '/sye',
        children: [{
          path: '/sye',
          name: '首页',
          component: () => import('@/views/dashboard/index'),
          meta: { title: '首页', icon: 'el-icon-s-data' }
        }]
      }, */
    /*   {
        path: '/test',
        component: Layout,
        redirect: '/index',
        children: [{
          path: '/index',
          name: '测试',
          component: () => import('@/views/test/role'),
          meta: { title: '测试', icon: 'el-icon-s-data' }
        }]
      }, */
    /* 
      {
        path: '/admin',
        component: Layout,
        redirect: '/admin/user',
        name: '系统管理',
        meta: { title: '系统管理', icon: 'el-icon-s-help' },
        children: [
          {
            path: 'user',
            name: '管理员',
            component: () => import('@/views/object/index'),
            meta: { title: '管理员', icon: 'table' }
          },
          {
            path: 'role',
            name: '角色',
            component: () => import('@/views/object/role'),
            meta: { title: '角色', icon: 'tree' }
          }
          ,
          {
            path: 'log',
            name: '日志',
            component: () => import('@/views/log/log'),
            meta: { title: '日志', icon: 'tree' }
          }
        ]
      } */
]



const createRouter = () => new Router({
    mode: 'history', // require service support
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes
})

const router = createRouter()




// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
    const newRouter = createRouter()
    router.matcher = newRouter.matcher // reset router

}
/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
//异步挂载的路由
//动态需要根据权限加载的路由表 
export function getMes() {
    router.options.routes = []
    var conrouts = []
    routing.findByIdRouting(getUserId()).then(res => {
        var datas = []
        datas = res.data.data;
        generaMenu(conrouts, datas)
        conrouts.push({ path: '*', redirect: '/404', hidden: true })
            /**
     conrouts.push(  {
      path: '/chatroom',
       component: Layout, 
       redirect: '/chatroom/index',
       children: [{
        path: '/chatroom/index',
        name: '聊天室',
        component: () => import('@/views/chatroom/index'),
        meta: { title: '聊天室', icon: 'el-icon-chat-round', }
      }]
    }) */
        router.addRoutes(conrouts)
        conrouts.forEach(item => {
            router.options.routes.push(item)
        })
    })

}
export function generaMenu(conrouts, data) {

    data.forEach(item => {
        if (item == null) {
            return;
        }
        /* 
        
    {
      path: '/admin',
      component: Layout,
      redirect: '/admin/user',
      name: '系统管理',
      meta: { title: '系统管理', icon: 'el-icon-s-help' },
      children: [
        {
          path: 'user',
          name: '管理员',
          component: () => import('@/views/object/index'),
          meta: { title: '管理员', icon: 'table' }
        }
      ]
    }    
        */
        const menu = {
            path: item.path,
            children: [],
            meta: { title: item.menuName, name: item.menuName, icon: item.icon, id: item.id, }
        }
        if (item.hidden == 0) {
            menu.hidden = item.hidden == 0
        }
        if (item.menuName) {
            menu.name = item.menuName
        }
        if (item.parentId == 0) {
            if (item.redirect) {
                menu.redirect = item.redirect
            }
            menu.component = Layout
        } else {
            menu.component = (resolve) => require([`@/views/${item.componentUrl}`], resolve) // 同步的方式

            // (resolve) => require([`@/views/${view}`], resolve)  import 是强绑定，require是浅复制，仅仅是普通的值传递
        }
        if (item.children != null && item.children.length != 0) {
            generaMenu(menu.children, item.children)
        }
        conrouts.push(menu)
    })
}

export default router